package com.whu.edu.zqm.assignments4.service;

import com.whu.edu.zqm.assignments4.dao.GoodsJPARepository;
import com.whu.edu.zqm.assignments4.entity.GoodsItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import java.util.*;

/**
 * @author jiaxy
 * 待办事项业务服务类。 这里只是演示JPA能力，所以这里代码写的很薄。实际项目中会复杂的多，项目核心代码都会在service层中。
 */
@Service

public class GoodService {


    private Map<Long, GoodsItem> GoodsMap =
            Collections.synchronizedMap(new HashMap<Long, GoodsItem>());


    public GoodsItem addGoods(GoodsItem good) {
        GoodsMap.put(good.getId(), good);
        return good;
    }


    public GoodsItem getGoods(long id) {
        return GoodsMap.get(id);
    }


    public List<GoodsItem> findGoods(String name, int num) {
        List<GoodsItem> result=new ArrayList<>();
        for (GoodsItem good: GoodsMap.values()){
            if (name!=null && !good.getName().contains(name)) {
                continue;
            }
            if (num!=0 && good.getNum()>= num) {
                continue;
            }
            result.add(good);
        }
        return result;
    }


    public void updateGoods(long id, GoodsItem good) {
        GoodsItem goodsItem  = GoodsMap.get(id);
        if(goodsItem!=null){
            GoodsMap.put(good.getId(),good);
        }
    }


    public void deleteGoods(long id) {
        GoodsMap.remove(id);
    }


    public void deleteAll(){
        GoodsMap.clear();
    }


}
